package com.mayn.web.express;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.core.Controller;
import com.mayn.action.KdniaoAction;
import com.mayn.api.KdniaoApi;
import com.mayn.api.SFApi;

import net.sf.json.JSONObject;

/**
 * ExpressController
 */
public class ExpressController extends Controller {

	private static final Logger LOG = LoggerFactory.getLogger(ExpressController.class);

	public void index() {
		renderText("Hello World");
	}

	/**
	 * 快递鸟订阅API调用 http://127.0.0.1:88/express/KdniaoSubscribe?expCode=EMS&expNo=
	 * 9504116340500
	 * http://120.26.195.93/JST-API/express/KdniaoSubscribe?expCode=EMS&expNo=
	 * 9504116340500
	 */
	public void KdniaoSubscribe() {
		if ("GET".equals(getRequest().getMethod())) {
			String expCode = "";
			String expNo = "";
			if (getPara("expCode") != null && !getPara("expCode").equals("")) {
				expCode = getPara("expCode");
			} else {
				renderText("Get参数错误:expCode error");
				return;
			}
			if (getPara("expNo") != null && !getPara("expNo").equals("")) {
				expNo = getPara("expNo");
			} else {
				renderText("Get参数错误:expNo error");
				return;
			}
			if (KdniaoAction.SubscribeExpressInfo(expCode, expNo)) {
				renderText("Success");
			} else {
				renderText("failed");
			}
		} else {
			renderText("无效的GET请求");
		}

	}

	/**
	 * 本地快递信息即时查询API调用
	 * http://127.0.0.1:88/express/LocalExpressSearch?expCode=EMS&expNo=
	 * 9504116340500
	 * http://120.26.195.93/JST-API/express/LocalExpressSearch?expCode=EMS&expNo
	 * =9504116340500
	 */
	public void LocalExpressSearch() {
		if ("GET".equals(getRequest().getMethod())) {
			String expCode = "";
			String expNo = "";
			if (getPara("expCode") != null && !getPara("expCode").equals("")) {
				expCode = getPara("expCode");
			} else {
				renderText("Get参数错误:expCode error");
				return;
			}
			if (getPara("expNo") != null && !getPara("expNo").equals("")) {
				expNo = getPara("expNo");
			} else {
				renderText("Get参数错误:expNo error");
				return;
			}

			try {
				JSONObject result = KdniaoAction.getLocalExpressInfo(expCode, expNo);
				renderJson(result);
			} catch (Exception e) {
				LOG.error(e.getMessage());
				renderText("本地快递即时查询API调用错误:expCode=" + expCode + ",expNo=" + expNo);
			}

		} else {
			renderText("无效的GET请求");
		}

	}

	/**
	 * 快递鸟即时查询API调用
	 * http://127.0.0.1:88/express/KdniaoSearch?expCode=EMS&expNo=9504116340500
	 * http://120.26.195.93/JST-API/express/KdniaoSearch?expCode=EMS&expNo=
	 * 9504116340500
	 */
	public void KdniaoSearch() {
		if ("GET".equals(getRequest().getMethod())) {
			String expCode = "";
			String expNo = "";
			if (getPara("expCode") != null && !getPara("expCode").equals("")) {
				expCode = getPara("expCode");
			} else {
				renderText("Get参数错误:expCode error");
				return;
			}
			if (getPara("expNo") != null && !getPara("expNo").equals("")) {
				expNo = getPara("expNo");
			} else {
				renderText("Get参数错误:expNo error");
				return;
			}
			KdniaoApi api = new KdniaoApi();
			try {
				String result = api.getOrderTracesByJson(expCode, expNo);
				renderJson(result);
			} catch (Exception e) {
				LOG.error(e.getMessage());
				renderText("快递鸟即时查询API调用错误:expCode=" + expCode + ",expNo=" + expNo);
			}

		} else {
			renderText("无效的GET请求");
		}

	}

	public void KdniaoSubscribeTest() {
		if ("GET".equals(getRequest().getMethod())) {
			// EMS
			KdniaoAction.SubscribeExpressInfo("EMS", "9715906333001");
			// SF 顺丰快递
			KdniaoAction.SubscribeExpressInfo("SF", "976872647980");
			// SURE 速尔快递
			KdniaoAction.SubscribeExpressInfo("SURE", "880144357615");
			// UC 优速快递
			KdniaoAction.SubscribeExpressInfo("UC", "518047710383");
			// STO 申通快递
			KdniaoAction.SubscribeExpressInfo("STO", "518047710383");
			// YTO 圆通快递
			KdniaoAction.SubscribeExpressInfo("YTO", "710167930984");
			// ZTO 中通速递
			KdniaoAction.SubscribeExpressInfo("ZTO", "719114360485");
			// YD 韵达快递
			KdniaoAction.SubscribeExpressInfo("YD", "1201355217916");
			// HHTT 天天快递
			KdniaoAction.SubscribeExpressInfo("HHTT", "550408420601");
			// DBL 德邦
			KdniaoAction.SubscribeExpressInfo("DBL", "5060491893");
			// HTKY 百世汇通
			KdniaoAction.SubscribeExpressInfo("HTKY", "211053123709");

			renderText("Success");
		} else {
			renderText("无效的GET请求");
		}

	}

	/**
	 * 快递鸟推送接收API
	 */
	public void KdniaoApply() {
		if ("GET".equals(getRequest().getMethod())) {
			if (getPara("test") != null && getPara("test").equals("nmgd")) {
				renderText("nmgd");
			} else {
				renderText("无效的POST请求");
			}
			return;
		}
		LOG.info("接收到快递鸟推送请求");
		String RequestData = "";
		try {
			// 特殊处理
			String Parameter = getRequest().getParameterMap().toString();
			if (Parameter.endsWith("=[]}")) {
				Parameter = Parameter.substring(1, Parameter.length() - 4);
				Parameter = Parameter.replaceAll("RequestData=", "");
				RequestData = Parameter;
			} else {
				RequestData = getPara("RequestData");
			}
			if (RequestData == null || RequestData.equals("")) {
				LOG.error("POST参数错误:RequestData error");
			} else {
				// LOG.debug("RequestData=" + RequestData);
			}
			JSONObject data = JSONObject.fromObject(RequestData);
			String EBusinessID = data.getString("EBusinessID");
			String PushTime = data.getString("PushTime");
			String Count = data.getString("Count");
			String Data = data.getString("Data");
			if (EBusinessID == null || EBusinessID.equals("")) {
				LOG.error("POST参数错误:EBusinessID error");
			} else {
				// LOG.info("EBusinessID=" + EBusinessID);
			}
			if (PushTime == null || PushTime.equals("")) {
				LOG.error("POST参数错误:PushTime error");
			} else {
				// LOG.info("PushTime=" + PushTime);
			}
			if (Count == null || Count.equals("")) {
				LOG.error("POST参数错误:Count error");
			} else {
				// LOG.info("Count=" + Count);
			}
			if (Data == null || Data.equals("")) {
				LOG.error("POST参数错误:Data error");
			} else {
				// LOG.info("Datas=" + Data);
			}
			renderJson(KdniaoAction.KdniaoApply(EBusinessID, PushTime, Count, Data));
		} catch (Exception e) {
			String msg = "推送API调用出错:" + e.getMessage();
			LOG.error(msg);
			renderText(msg);
		}
	}

	/*-------------------------*/
	/**
	 * 顺丰即时查询API调用
	 * http://127.0.0.1:88/express/SFSearch?expNo=972246426499
	 * http://120.26.195.93/JST-API/express/SFSearch?expNo=972246426499
	 * 9504116340500
	 */
	public void SFSearch() {
		if ("GET".equals(getRequest().getMethod())) {
			String expNo = "";
			if (getPara("expNo") != null && !getPara("expNo").equals("")) {
				expNo = getPara("expNo");
			} else {
				renderText("Get参数错误:expNo error");
				return;
			}
			KdniaoApi api = new KdniaoApi();

			try {
				String result = SFApi.getSfTraceJson(expNo);
				renderJson(result);
			} catch (Exception e) {
				LOG.error(e.getMessage());
				renderText("顺丰即时查询API调用错误:expNo=" + expNo);
			}

		} else {
			renderText("无效的GET请求");
		}

	}
	
	

}
